/*
 * Copyright 2011 Mark Davidson.
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */

package org.dctmcommons.impl;

import org.dctmcommons.IParamCheck;
import org.dctmcommons.IQueryHelper;
import org.dctmcommons.ISecurityHelper;

import com.documentum.fc.client.IDfACL;
import com.documentum.fc.client.IDfQuery;
import com.documentum.fc.client.IDfSession;
import com.documentum.fc.common.DfException;

/**
 * Contains security-related helper methods.
 */
public class SecurityHelper implements ISecurityHelper {

	/** DQL template for checking if folder security is enabled. */
	private static final String DQL_FOLDER_SECURITY =
								"select folder_security from dm_docbase_config";

	/** Token for the ACL name. */
	private final static String TOKEN_ACL_NAME = "ACL_NAME";
	
	/** DQL template for getting an ACL by object name. */
	private final static String DQL_GET_ACL_BY_NAME =
		"dm_acl where object_name = '" + TOKEN_ACL_NAME + "'";
	
	/** Token for the qualification ending. */
	private final static String TOKEN_QUAL_END = "QUAL_END";
	
	/** DQL template for getting an ACL by qualification. */
	private final static String DQL_GET_ACL_BY_QUAL =
		"dm_acl where " + TOKEN_QUAL_END;
	
	/** The query helper. */
	private IQueryHelper queryHelper = null;
	
	/** The parameter check instance. */
	private static final IParamCheck paramCheck = ParamCheck.getInstance();

	/**
	 * Default constructor.
	 */
	private SecurityHelper() {
		queryHelper = QueryHelper.getInstance();
	}

	/**
	 * Factory method.
	 * @return A new security helper instance.
	 */
	public static ISecurityHelper getInstance() {
		return new SecurityHelper();
	}
	
	/* (non-Javadoc)
	 * @see org.dctmcommons.impl.ISecurityHelper#isFolderSecurityEnabled(com.documentum.fc.client.IDfSession)
	 */
	@Override
	public boolean isFolderSecurityEnabled(final IDfSession session)
		throws DfException {
		paramCheck.checkObject(session, "session");
		
		return queryHelper.queryForSingleBooleanValue(
				session,
				DQL_FOLDER_SECURITY,
				IDfQuery.DF_READ_QUERY);
	}

	/*
	 * (non-Javadoc)
	 * @see org.dctmcommons.ISecurityHelper#getAclByName(com.documentum.fc.client.IDfSession, java.lang.String)
	 */
	@Override
	public IDfACL getAclByName(final IDfSession session, final String aclName)
			throws DfException {
		
		paramCheck.checkObject(session, "session");
		paramCheck.checkString(aclName, "aclName");
		
		final String dql = DQL_GET_ACL_BY_NAME.replaceFirst(TOKEN_ACL_NAME,
															aclName);
		return (IDfACL) session.getObjectByQualification(dql);
	}

	/*
	 * (non-Javadoc)
	 * @see org.dctmcommons.ISecurityHelper#getAclByQualification(com.documentum.fc.client.IDfSession, java.lang.String)
	 */
	@Override
	public IDfACL getAclByQualification(final IDfSession session,
										final String qualification)
			throws DfException {
		
		paramCheck.checkObject(session, "session");
		paramCheck.checkString(qualification, "qualification");
		
		final String dql = DQL_GET_ACL_BY_QUAL.replaceFirst(TOKEN_QUAL_END,
															qualification);
		return (IDfACL) session.getObjectByQualification(dql);
	}
}
